<template>
  <el-dialog :title="title" :close-on-click-modal="false" :fullscreen="true" :visible.sync="visible"
    :before-close="cancel" class="JNPF-dialog JNPF-dialog_center" lock-scroll width="50%" append-to-body>
    <el-tabs type="border-card">
      <el-tab-pane label="单据详情" :style="{'height':screenHeight-220+'px'}">
        <el-row class="JNPF-common-search-box" :gutter="16">
          <el-form @submit.native.prevent ref="elForm" :model="dataForm">
            <el-col :span="8">
              <el-form-item label="单据编号" prop="fbillno">
                <el-input v-model="dataForm.fbillno" placeholder="自动生成" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="采购日期" prop="forddate">
                <el-date-picker
                    v-model="dataForm.forddate"
                    type="date"
                    placeholder="选择日期">
                  </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="单据状态" prop="fbillstate">
                <el-select v-model="dataForm.fbillstate" placeholder="请选择">
                <el-option
                  v-for="item in selectlist2"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="学校名称" prop="fsiteNo">
                <el-input v-model="dataForm.fsiteNo" placeholder="自动生成" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="采购人员" prop="forduser">
                <el-select v-model="dataForm.forduser" placeholder="请选择">
                <el-option
                  v-for="item in cguser"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="到货日期" prop="fplanrecdate">
                <el-date-picker
                    v-model="dataForm.fplanrecdate"
                    type="date"
                    placeholder="选择日期">
                  </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="供应商" prop="fsupno">
                <el-select v-model="dataForm.fsupno" placeholder="请选择">
                <el-option
                  v-for="item in gyuser"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="备注" prop="fexplanation">
                <el-input v-model="dataForm.fexplanation" placeholder="备注"/>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </el-tab-pane>
      <el-tab-pane label="单据明细">
        <el-table :data="list" border stripe :height="screenHeight-260">
          <el-table-column label="操作" width="80">
            <template slot-scope="scope">
              <el-button slot="reference" icon="el-icon-delete" style="color: red"  @click="shopdate(scope.$index)"
                type="text">删除</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center"  label="设备编号"  prop="feuipmentid"/>
          <el-table-column align="center" label="采购价" prop="fbuyprice" />
          <el-table-column align="center" label="数量" prop="fqty">
            <template slot-scope="scope">
              <el-input placeholder="请输入内容" v-model="scope.row.fqty" @change="sunamt(scope.$index)"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" label="合计" prop="ftotamt" />
          <el-table-column align="center" label="备注" prop="fmemo">
            <template slot-scope="scope">
              <el-input placeholder="请输入内容" v-model="scope.row.fmemo"></el-input>
            </template>
          </el-table-column>
        </el-table>
        <div class="addbox">
          <el-link icon="el-icon-plus" type="primary" @click="insert">添加一行</el-link>
        </div>
      </el-tab-pane>
    </el-tabs>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary"  :loading="loading" v-if="insertorupdate" @click="HandelCreate">保存</el-button>
      <el-button type="primary"  :loading="loading" v-else @click="HandelupdateRoom">保存</el-button>
    </span>
    <Device ref="Device"  @confirm="shoplist"/>
  </el-dialog>
</template>
<style scoped>
  .addbox {
    text-align: center;
    padding: 10px 0;
  }
</style>
<script>
import {createordHead,updateordHead} from "@/api/purchase/ordHead"
import Device from "@/components/Device/index.vue"
  export default {
    props:{
      cguser:{
        type:Array,
        default:[]
      },
      gyuser:{
        type:Array,
        default:[]
      }
    },
    components:{Device},
    data() {
      return {
        title: "新增",
        insertorupdate: true,
        screenHeight: document.body.clientHeight,
        dataForm: {
          fbillno:undefined,
          forddate:undefined,
          fbillstate:undefined,
          fsiteNo:"300017",
          forduser:undefined,
          fplanrecdate:undefined,
          fsupno:undefined,
          fexplanation:undefined,
        },
        selectlist2:[
          {
            "label": "待提交",
            "value": "1"
          },
          {
            "label": "已提交",
            "value": "2"
          },
          {
            "label": "已作废",
            "value": "8"
          }
        ],
        list: [],
        loading: false,
        visible: false,
      };
    },
    created() {

    },
    methods: {
      //
      init(FBILLNO) {
        this.dataForm.FBILLNO = FBILLNO;
        if (this.dataForm.FBILLNO) {
          this.title = "修改"
          this.insertorupdate = false;
        }
        else {
          this.title = "新增"
          this.insertorupdate = true;
        }
        this.visible = true;
      },
      insert() {
        this.$refs.Device.open();
      },
      shopdate(id) {
        this.list.splice(id, 1)
      },
      shoplist(data){
        this.list.push({
          feuipmentid: data.fequipmentno,
          fname: data.fequipmentname,
          fbuyprice: data.fbuyprice,
          fqty: 0,
          ftotamt: 0,
          fmemo: ""
        })
      },
      //
      HandelCreate(){
        this.$refs["elForm"].validate((valid) => {
          if (valid) {
            this.dataForm.stcOrdDetails=this.list;
            createordHead(this.dataForm)
            .then(res=>{
              this.$message({
                message: '添加成功',
                type: 'success'
              });
              this.cancel();
            })
          }
        })
      },
      HandelupdateRoom(){
        this.$refs["elForm"].validate((valid) => {
          if (valid) {
            this.dataForm.stcOrdDetails=this.list;
            updateordHead(this.dataForm)
            .then(res=>{
              this.$message({
                message: '修改成功',
                type: 'success'
              });
              this.cancel();
            })
          }
        })
      },
      //
      sunamt(id){
        this.list[id].ftotamt=parseFloat(this.list[id].fqty*this.list[id].fbuyprice).toFixed(2)
      },
      //
      cancel() {
        this.$refs.elForm.resetFields();
        this.list = [];
        this.visible = false;
      },
    },
  };
</script>